package com.cb.common.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

/**
 * <p>
 * 	对用户行为信息url进行读取
 * </p>
 * @version 1.0
 */
public class MsgUrlConfigUtil{
	
	protected Map configMap=null;
	protected ServletContext servletContext = null;
	
	protected final static MsgUrlConfigUtil configUtil = new MsgUrlConfigUtil();

	protected MsgUrlConfigUtil() {
		
    }

	public static MsgUrlConfigUtil getInstance() {
        return configUtil;
    }
	
   
	
    /**
     * 返回日志路径
     * @return
     */
    public String getMsgURL(String queryString){
    	String url=(String)configMap.get(queryString);
    	return url;
    }

	public void init(ServletContext servletContext) {
		this.servletContext=servletContext;
		configMap=new HashMap();
    	//读取来自config.properties文件的配置,并且放入configMap内,应用程序共同使用
    	String filePath=servletContext.getRealPath("/WEB-INF/classes/conf/");
    	
    	Properties props=new Properties();
    	InputStream is=null;
    	try{
    		is=new BufferedInputStream(new FileInputStream(filePath+"/msgurl.properties"));
    		props.load(is);
    		Iterator it= props.keySet().iterator();
    		while(it.hasNext()){
    			String key=(String)it.next();
    			configMap.put(key, props.get(key));
    		}
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}finally{
    		try{
    			if(is!=null){
    				is.close();
    			}
    		}catch(Exception ex){}
    	}
		
	}
}
